package com.mars.tukuan.rpc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mars.base.common.utils.Paginator;
import com.mars.base.service.support.BaseService;
import com.mars.tukuan.dao.mapper.CarRepairMapper;
import com.mars.tukuan.dto.CarRepairVo;
import com.mars.tukuan.entity.CarRepair;
import com.mars.tukuan.rpc.api.CarRepairService;

@Service
public class CarRepairServiceImpl extends BaseService<CarRepair, Long> implements CarRepairService {

	@Autowired
	private CarRepairMapper mapper ;
	
	@Override
	public void saveOrUpdate(CarRepairVo vo) {
		//根据查了ID查询，判断记录是否存在
		Condition condition = new Condition(CarRepair.class) ;
		condition.createCriteria().andEqualTo("carId", vo.getId());
		List<CarRepair> entitys = this.mapper.selectByCondition(condition);
		
		if(null != entitys && !entitys.isEmpty()) {//记录存在，则修改
			CarRepair carRepair = entitys.get(0) ;
			carRepair.setCarNextRepair(vo.getCarNextRepair());
			super.updateSelective(carRepair) ;
		} else {//记录不存在，则添加
			CarRepair entity = new CarRepair() ;
			entity.setCarId(vo.getId());
			entity.setCarNextRepair(vo.getCarNextRepair());
			super.insertSelective(entity) ;
		}
	}
	
	@Override
	public void updateCurrent(CarRepairVo vo) {
		//根据查了ID查询，判断记录是否存在
		Condition condition = new Condition(CarRepair.class) ;
		condition.createCriteria().andEqualTo("carId", vo.getId());
		List<CarRepair> entitys = this.mapper.selectByCondition(condition);
		
		if(null != entitys && !entitys.isEmpty()) {//记录存在，则修改
			CarRepair carRepair = entitys.get(0) ;
			carRepair.setCarCurrMileage(vo.getCarCurrMileage());
			super.updateSelective(carRepair) ;
		} else {//记录不存在，则添加
			CarRepair entity = new CarRepair() ;
			entity.setCarId(vo.getId());
			entity.setCarCurrMileage(vo.getCarCurrMileage());
			super.insertSelective(entity) ;
		}
	}

	@Override
	public PageInfo<CarRepairVo> selectPageByCar(Paginator paginator, CarRepairVo vo) {
		PageHelper.startPage(paginator.getPage(), paginator.getRows()) ;
		Page<CarRepairVo> page = this.mapper.selectPageByCar(paginator, vo) ;
		
		return new PageInfo<CarRepairVo>(page) ;
	}
	
	
}
